﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
using System.ComponentModel.Composition;

namespace CodeC.Utilities
{
    /// <summary>
    /// This class handle File IO operation
    /// </summary>
    [Export]
    public class FileOperations
    {
        #region "Properties"
        private string _tempFoler;

        private string TempFolder
        {
            get
            {
                if (null == _tempFoler)
                    _tempFoler = DateTime.Now.Ticks.ToString();
                return _tempFoler;
            }
        }

        #endregion

        #region "Methods"

        public string CreateXMLDB(string fileName)
        {
            string directory = FolderFullName("data");
            string fileNameWithOutExtension = Path.GetFileNameWithoutExtension(fileName);
            string extension = Path.GetExtension(fileName);

            fileName = string.Format("{0}{3}{1}{2}",
                   directory,
                   fileNameWithOutExtension,
                   extension,
                   string.IsNullOrWhiteSpace(directory) ? "" : Path.DirectorySeparatorChar.ToString());

            if (!File.Exists(fileName))
            {
                (new DataTable("CODECXMLDB")).WriteXml(fileName);
            }

            return fileName;
        }

        private string FolderFullName(string folder, string directory = "")
        {
            if (directory.IsNotNullOrNotEmpty())
                directory = directory + Path.DirectorySeparatorChar.ToString() + folder.ToString().RemoveSpecialChar();
            else
                directory = folder.ToString().RemoveSpecialChar();

            if (!Directory.Exists(directory))
                directory = Directory.CreateDirectory(directory).FullName;
            else
                directory = (new DirectoryInfo(directory)).FullName;

            return directory;
        }

        private string GetFileName(string fileName)
        {
            string directory = FolderFullName("data"); 
            string fileNameWithOutExtension = Path.GetFileNameWithoutExtension(fileName);
            string extension = Path.GetExtension(fileName);

            fileName = string.Format("{0}{3}{1}{2}",
                   directory,
                   fileNameWithOutExtension,
                   extension,
                   string.IsNullOrWhiteSpace(directory) ? "" : Path.DirectorySeparatorChar.ToString());

            return fileName;
        }

        public string Write(string fileName, string content)
        {

            int i = 0;
            string directory = FolderFullName("data");  
            string fileNameWithOutExtension = Path.GetFileNameWithoutExtension(fileName);
            string extension = Path.GetExtension(fileName);

            fileName = string.Format("{0}{3}{1}{2}",
                   directory,
                   fileNameWithOutExtension,
                   extension,
                   string.IsNullOrWhiteSpace(directory) ? "" : Path.DirectorySeparatorChar.ToString());

            while (File.Exists(fileName))
                fileName = string.Format("{0}{3}{1}{2}",
                    directory,
                    fileNameWithOutExtension + ++i,
                    extension,
                    string.IsNullOrWhiteSpace(directory) ? "" : Path.DirectorySeparatorChar.ToString());

            using (StreamWriter outfile = new StreamWriter(fileName))
            {
                outfile.Write(content);
                outfile.Close();
            }

            return fileName;
        }

        public string Read(string fileName)
        {
            string output = string.Empty;

            fileName = GetFileName(fileName);


            if (File.Exists(fileName))
            {
                using (StreamReader streamReader = new StreamReader(fileName))
                {
                    output = streamReader.ReadToEnd();
                }
            }

            return output;
        }
        /// <summary>
        /// Delete a file
        /// </summary>
        /// <param name="fileName">File Name and Path</param>
        /// <returns></returns>
        public bool Delete(string fileName)
        {
            fileName = GetFileName(fileName);

            if (File.Exists(fileName))
            {
                File.Delete(fileName);
                return true;
            }


            return false;
        }
        #endregion

    }
}
